package com.heima.context;

import java.util.HashMap;
import java.util.Map;

public class BaseContext {

   public static ThreadLocal<Map<String, Object>> threadLocal = new ThreadLocal<>();

   public static void setData(String key, Object value) {
       Map<String, Object> map = threadLocal.get();
       if (map == null) {  // 检查 map 是否为空
           map = new HashMap<>();
           threadLocal.set(map);  // 初始化并设置到 ThreadLocal 中
       }
       map.put(key, value);
   }

   public static Object getData(String key) {
       return threadLocal.get().get(key);
   }

   public static void removeData(String key) {
       threadLocal.get().remove(key);
   }

   public static void clear() {
       threadLocal.remove();
   }

}
